<template>
  <div class="components-input-demo-presuffix">

    <a-row :span="24">
      <a-form-item label="方案"  :labelCol="{span:3}" :wrapperCol="{span:20}">
        <a-input @click="openModal" placeholder="请点击选择方案" v-model="departNames" readOnly :disabled="disabled">
          <a-icon slot="prefix" type="cluster" title="方案选择控件"/>
        </a-input>
      </a-form-item>
    </a-row>
    <a-row :span="24" v-show="this.planList.length>0"  >
      <a-col span="3" align="right">
        <div class="ant-form-item-label">
          <label>已选方案</label>
        </div>
      </a-col>

      <a-col  span="18" >
        <a-tag   v-for="(item,index) in this.planList" :key="item.title" closable @close="removePlan(index)">
          {{item.title}}
        </a-tag>
      </a-col>
    </a-row>


    <j-select-plan-modal
      ref="innerPlanSelectModal"
      :modal-width="modalWidth"
      :multi="multi"
      :rootOpened="rootOpened"
      @ok="handleOK"
      />
  </div>
</template>

<script>
  import JSelectPlanModal from '../modal/JSelectPlanModal.vue'
  export default {
    name: 'JSelectPlan',
    components:{
      JSelectPlanModal
    },
    props:{
      modalWidth:{
        type:Number,
        default:'80%',
        required:false
      },
      multi:{
        type:Boolean,
        default:false,
        required:false
      },
      rootOpened:{
        type:Boolean,
        default:true,
        required:false
      },
      value:{
        type:String,
        required:false
      },
      disabled:{
        type: Boolean,
        required: false,
        default: false
      },
      // 自定义返回字段，默认返回 id
      customReturnField: {
        type: String,
        default: 'id'
      }
    },
    data(){
      return {
        departNames:"",
        departIds:'',
        planList:[]
      }
    },
    mounted(){
    },
    watch:{
      value(val){
        if (val instanceof Array) {
          this.planList = val;
        } else {
          this.planList.push(val);
        }

      }
    },
    methods:{

      openModal(){
        this.$refs.innerPlanSelectModal.show()
      },
      removePlan(v){
        this.planList.splice(v,1);
        this.$emit("change",  this.planList);
      },
      handleOK(rows) {
        if (rows && rows.length > 0) {
          this.planList = rows;
        }else{
         this.planList = [];
        }
        this.$emit("change",  this.planList)
      },

      handleEmpty(){
        this.handleOK('')
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    }
  }
</script>

<style scoped>
  .components-input-demo-presuffix .anticon-close-circle {
    cursor: pointer;
    color: #ccc;
    transition: color 0.3s;
    font-size: 12px;
  }
  .components-input-demo-presuffix .anticon-close-circle:hover {
    color: #f5222d;
  }
  .components-input-demo-presuffix .anticon-close-circle:active {
    color: #666;
  }
</style>